﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

using Telerik.Windows.Controls;
using SaleLibrary.Score;
using BrokerBase.Lib.Core;
using SaleLibrary.License;
using NHibernate;

namespace Broker.Mgr.SaleView
{
    /// <summary>
    /// Interaction logic for ScorePolicyForm.xaml
    /// </summary>
    public partial class ScorePolicyForm
    {
        private IScorePolicy policy;

        public ScorePolicyForm(IScorePolicy policy)
        {
            InitializeComponent();
            this.policy = policy;

            Loaded += new RoutedEventHandler(ScorePolicyForm_Loaded);
        }

        void ScorePolicyForm_Loaded(object sender, RoutedEventArgs e)
        {
            SetIsCheck();
            DataContext = policy;
        }

        private void btnSave_Click(object sender, RoutedEventArgs e)
        {
            this.Cursor = Cursors.Wait;
            try
            {
                IBaseService<ScorePolicy> svr = new BaseService<ScorePolicy>(AppGlobal.HbmSessionFactory.GetCurrentSession());

                policy.Used = true;
                svr.SaveOrUpdate(policy as ScorePolicy);

                MessageBox.Show("保存成功");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "错误提示");
            }
            finally
            {
                this.Cursor = Cursors.Arrow;
            }
        }

        private void btnExit_Click(object sender, RoutedEventArgs e)
        {
            Close();
        }

        private void btnAddRule_Click(object sender, RoutedEventArgs e)
        {
            IScoreRule rule = new ScoreRule();
            rule.Policy = policy;
            rule.Scale = 0.5;
            rule.Score = 40;
            rule.MaxLevel = 8;
            rule.MinAmount = 199;
            
            ScoreRuleForm frm = new ScoreRuleForm(rule);

            frm.WindowStartupLocation = WindowStartupLocation.CenterOwner;
            frm.ShowDialog();

            if (frm.IsCommit)
            {
                policy.Rules.Add(rule);
                gvRule.ItemsSource = null;
                gvRule.ItemsSource = policy.Rules;
                gvRule.Items.Refresh();
            }

        }

        private void btnEditRule_Click(object sender, RoutedEventArgs e)
        {
            ViewRule();
        }

        private void btnDelRule_Click(object sender, RoutedEventArgs e)
        {
            IScoreRule rule = gvRule.SelectedItem as IScoreRule;

            if (rule != null)
            {
                policy.Rules.Remove(rule);
                gvRule.ItemsSource = null;
                gvRule.ItemsSource = policy.Rules;
            }
        }

        private void gvRule_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            ViewRule();
        }

        private void ViewRule()
        {
            IScoreRule rule = gvRule.SelectedItem as IScoreRule;

            if (rule != null)
            {
                ScoreRuleForm frm = new ScoreRuleForm(rule);

                frm.WindowStartupLocation = WindowStartupLocation.CenterOwner;
                frm.ShowDialog();
            }
        }


        private void SetIsCheck()
        {
            if (policy.Id > 0)
            {
                try
                {
                    ISession sess = AppGlobal.HbmSessionFactory.GetCurrentSession();

                    long count = sess.CreateQuery(string.Format("select count(*) from ActiveCode as a where a.Policy.Id={0}", policy.Id)).UniqueResult<long>();

                    policy.IsCheck = count > 0 ? true : false;
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "提示");
                }
            }
        }
        
    }
}
